import {get, post, upload} from "./http";
const FormData = require('form-data');

export const _get = function(url, data, success, error) {
  get(url, data)
    .then(res => {
      success(res);
    })
    .catch(err => {
      error && error(err);
    });
};

export const _post = function (url, data, success, error) {
  post(url, data).then(res => {
    success(res);
  })
    .catch(err => {
      console.log(error);
      error && error(err);
    });
};

export const logout = (success, error) => {
  _post('ajax/logout', {}, success, error)
}

export const teacherModel = {
  pre: '/user/teacher',
  list: function (data, success, error) {
    _get(this.pre + 's', data, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  batchBlock: function(data, success, error) {
    _post('/user/teacher/batch/block', data, success, error)
  },
  item:function(data, success, error) {
    _get(this.pre + '/view/' + data, {}, success, error)
  },
}

export const studentModel =  {
  pre: '/user/student',
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  }, 
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  batchBlock: function(data, success, error) {
    _post('/user/student/batch/block', data, success, error)
  },
  item:function(data, success, error) {
    _get(this.pre + '/view/' + data, {}, success, error)
  }
}

export const expertModel =  {
  pre: '/user/expert',
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  }, 
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  batchBlock: function(data, success, error) {
    _post('/user/expert/batch/block', data, success, error)
  },
  item:function(data, success, error) {
    _get(this.pre + '/view/' + data, {},  success, error)
  }
}

export const collegeModel = {
  pre: '/object/college',
  list: function(data, success, error) {
    _get('/college/list', data, success, error)
  },
  objectList: function(data, success, error) {
    _get('/object/colleges', data, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  item: function(id, success, error) {
    _get(this.pre + '/' + id, {}, success, error)
  }
}

export const schoolModel = {
  pre: '/object/school',
  list: function(data, success, error) {
    _get('/school/list', data, success, error)
  },
  objectList: function(data, success, error) {
    _get('/object/schools', data, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  item: function(id, success, error) {
    _get(this.pre + '/' + id, {}, success, error)
  },
 
}

export const majorModel = {
  pre: '/object/major',
  list: function(data, success, error) {
    _get('/major/list', data, success, error)
  },
  objectList: function(data, success, error) {
    _get('/object/majors', data, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  item: function(id, success, error) {
    _get(this.pre + '/' + id, {}, success, error)
  }
}

export const classModel = {
  pre: '/object/class',
  search: function(data, success, error) {
    _get('/class/list', data, success, error)
  },
  list: function(data, success, error) {
    _get(this.pre + 'es', data, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  batchBlock: function(data, success, error) {
    _post('/object/class/batch/block', data, success, error)
  },
  batchAdd: function(data, success, error) {
    _post(this.pre + '/batch/add', data, success, error)
  },
  item: function(id, success, error) {
    _get(this.pre + '/' + id, {}, success, error)
  } 
}

export const courseModel = {
  pre: '/object/course',
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  },
  chapters: function(course_id, success, error) {
    _get(this.pre + '/chapter/' + course_id, {}, success, error)
  },
  chapterAdd: function(data, success, error) {
    _post(this.pre + '/chapter/add', data, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  item: function(data, success, error) {
    _get(this.pre + '/' + data, {}, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  batchBlock: function(data, success, error) {
    _post('/object/course/batch/block', data, success, error)
  },
  chapter: function(data, success, error) {
    _get('/object/chapter/' + data, {}, success, error)
  },
  chapterUpdate: function(id, data, success, error) {
    _post('/object/chapter/update/' + id, data, success, error)
  },
  students: function(id, success, error) {
    _get(this.pre + '/student/' + id, {}, success, error)
  },
  teachers: function(id, success, error) {
    _get(this.pre + '/teacher/' + id, {}, success, error)
  },
  studentAdd: function(data, success, error) {
    _post(this.pre + '/student/add', data, success, error)
  },
  teacherAdd: function(data, success, error) {
    _post(this.pre + '/teacher/add', data, success, error)
  }
}

export function _upload(file, success, error) {
  const form = new FormData();
  form.append('location', file);
  upload('/object/richtext/image/upload', form).then(res => {
    success(res);
  }).catch(err => {
    error(err)
  })
}

export const experimentModel = {
  pre: '/object/experiment',
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  },
  envs: function(data, success, error) {
    _get('/object/environments', data, success, error)
  },
  item: function(data, success, error) {
    _get(this.pre + '/' + data, {} , success, error)
  },
  env: function(data, success, error) {
    _get('/object/environment/' + data, {}, success, error)
  },
  envIns: function(data, success, error) {
    _get('/object/environment/instance/' + data, {}, success, error)
  },
  insBatchRec: function(data, success, error) {
    _post('/object/instance/batch/recovery', data, success, error)
  },
  insBatchSd: function(data, success, error) {
    _post('/object/instance/batch/shutdown', data, success, error)
  },
  envIssue: function(data, success, error) {
    _post('/object/environment/issue', data, success, error)
  }
}

export const imageModel = {
  pre: '/object/image',
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  },
  search: function(data, success, error) {
    _get(this.pre + '/list', data, success, error)
  },
  upload: function(data, success, error) {
    _post(this.pre + '/upload', data, success, error)
  },
  import: function(data, success, error) {
    _post(this.pre + '/import', data, success, error)
  },
  createImport: function(data, success, error) {
    _post(this.pre + '/createimport', data, success, error)
  },
  describe: function(data, success, error) {
    _post(this.pre + '/describe', data, success, error)
  },
  create1: function(data, success, error) {
    _post(this.pre + '/create/first', data, success, error)
  },
  create2: function(data, success, error) {
    _post(this.pre + '/create/second', data, success, error)
  },
  create3: function(data, success, error) {
    _post(this.pre + '/create/third', data, success, error)
  },
  modify1: function(data, success, error) {
    _post(this.pre + '/modify/first', data, success, error)
  },
  modify2: function(data, success, error) {
    _post(this.pre + '/modify/second', data, success, error)
  },
  modify3: function(data, success, error) {
    _post(this.pre + '/modify/third', data, success, error)
  },
  item: function(data, success, error) {
    _get(this.pre + '/' + data, {}, success, error)
  },
  batchRem: function(data, success, error) {
    _post(this.pre + '/batch/remove', data, success, error)
  },
}

export const kldModel = {
  pre: '/object/knowledge',
  search: function(data, success, error) {
    _get(this.pre + 's/search', data, success, error)
  },
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  }
}

export const cosModel = {
  getSTS: function(success, error) {
    _get('cos/sts', {}, success, error)
  }
}

export const adminModel = {
  list: function(data, success, error) {
    _get('admins', data, success, error)
  },
  item: function(data, success, error) {
    _get('admin/' + data, {}, success, error)
  },
  create: function(data, success, error) {
    _post('admin/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post('admin/update/' + id, data, success, error)
  },
  login: function( data, success, error) {
    _get('admin/login' ,data, success, error)
  },
}
export const orderModel = {
  list: function(data, success, error) {
    _get('orders', data, success, error)
  }
}

